﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AscendEast_DataEntry
{
    public partial class Form_School_Delete : Form
    {
        DataAccess dal = new DataAccess();
        public Form_School_Delete()
        {
            InitializeComponent();
            PopulauteComboBox_Schools();
        }

        private void PopulauteComboBox_Schools()
        {
            string select_schools_query = "select * from tbl_schools";
            DataTable dt0 = dal.Select(select_schools_query);
            DataTable dt = new DataTable();
            dt.Columns.Add("school_id", typeof(String));
            dt.Columns.Add("school_name", typeof(String));
            dt.Rows.Add("null", "Select School");
            for (int i = 0; i < dt0.Rows.Count; i++)
            {
                dt.Rows.Add(dt0.Rows[i][0], dt0.Rows[i][1]);
            }

            cb_school_name_delete_search.DisplayMember = dt.Columns["school_name"].ToString();
            cb_school_name_delete_search.ValueMember = dt.Columns["school_id"].ToString();
            cb_school_name_delete_search.DataSource = dt;
        }

        private void btn_search_delete_school_Click(object sender, EventArgs e)
        {
            if (cb_school_name_delete_search.SelectedIndex == 0)
                MessageBox.Show("Please select any School");
            else
            {
                int school_id = int.Parse(cb_school_name_delete_search.SelectedValue.ToString());
                PopulateDataGridView_Schools(school_id);
            }
        }

        private void PopulateDataGridView_Schools(int school_id)
        {
            string edit_schoool_query = "select * from tbl_schools where school_id = " + school_id;
            dgv_school_delete.DataSource = null;
            dgv_school_delete.Rows.Clear();
            dgv_school_delete.Refresh();
            DataTable dt = new DataTable();
            dt = dal.Select(edit_schoool_query);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dgv_school_delete.Rows.Add(dt.Rows[i][0],dt.Rows[i][1]);
            }

        }

        private void btn_delete_school_Click(object sender, EventArgs e)
        {
            if (cb_school_name_delete_search.SelectedIndex == 0)
                MessageBox.Show("Please select a School");
            else
            {
                if (MessageBox.Show("Do you want to delete?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    int school_id = int.Parse(cb_school_name_delete_search.SelectedValue.ToString());
                    String delete_school_query = "delete from tbl_schools where school_id = " + school_id;
                    bool deleted_flag = dal.Delete(delete_school_query);
                    
                    if (deleted_flag == true)
                        MessageBox.Show("Record Deleted Successfully");
                    else
                        MessageBox.Show("Try again");
                    
                    dgv_school_delete.DataSource = null;
                    dgv_school_delete.Rows.Clear();
                    dgv_school_delete.Refresh();
                    PopulauteComboBox_Schools();
                    school_id = 0;
                }
            }
        }

        private void btn_cancel_school_delete_Click(object sender, EventArgs e)
        {
            dgv_school_delete.DataSource = null;
            dgv_school_delete.Rows.Clear();
            dgv_school_delete.Refresh();
            cb_school_name_delete_search.SelectedIndex = 0;           
        }
    }
}
